<?php
/**
 * 转正流程
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Flow_UserRegular");
apf_require_class("Bll_Flow_OperateLog");
apf_require_class("Bll_Flow_ApplyRelation");
apf_require_class("Const_Flow");

class Flow_RegularController extends AbstractController {


    /**
     *
     * @var unknown_type
     */
    private $params;


    /**
     * 需要的数据
     * @var unknown_type
     */
    private $data;


    /**
     * 所需要的所有bll
     * @var unknown_type
     */
    private $bll=array();


    public function handle_request() {
        $this->bll['user_regular'] = new Bll_Flow_UserRegular();

        //检查参数
        $this->check_param();

        if( $this->request->is_post_method() ){//处理数据库
            $this->operate_db();
        }else{                                 //输出表单
            //获取数据
            $this->get_data();
            //检查权限
            $this->check_super_uid();
            $this->request->set_attribute('flow_info', $this->data['flow_info']);
            $this->request->set_attribute('user_info', $this->data['user_info']);
            $this->request->set_attribute('is_disable', $this->data['is_disable']);
            $this->request->set_attribute('is_super', $this->data['is_super']);
            return "Flow_Regular";
        }
    }

    /**
     * 参数检查
     */
    private function check_param(){
        $this->params = $this->request->get_parameters();
        if($this->request->is_post_method()){ //post
            if(!is_numeric($this->params['flow_id'])){
                $this->show_tips('ERROR_PARAM', Flow_Urls::flow_list());
            }
            if(!is_numeric($this->params['super_opinion'])){
                $this->show_tips('ERROR_PARAM', Flow_Urls::flow_list());
            }else{
                //不同意必须填写理由
                if( $this->params['super_opinion'] == 2 && empty($this->params['super_reason']) ){
                    $this->show_tips('ERROR_NO_SUPER_REASON',Flow_Urls::flow_regular($this->params['flow_id']));
                }
            }
        }else{                                //get
            if(!is_numeric($this->params['flow_id'])){
                $this->show_tips('ERROR_PARAM', Flow_Urls::flow_list());
            }
        }
        return;
    }


    /**
     * 获取表单需要的数据
     */
    private function get_data(){
        //转正流程信息
        $this->get_user_regular_info();
        //用户信息
        $this->get_userinfo();
    }


    /**
     * 检查是否是上级主管
     */
    private function check_super_uid(){
        $hr_bll = new Bll_Hr_OutInterface();
        $super_data = $hr_bll->get_under_leader($this->data['flow_info']['user_id']);
        $this->data['is_disable'] = '';
        if(empty($super_data) || $super_data['user_id'] != $this->user_info['user_id'] || date('Ymd') > date('Ymd',strtotime($this->data['flow_info']['effect_date']))){
            $this->data['is_disable'] = 'disabled';
        }
        $this->data['is_super'] = (!empty($super_data) && $super_data['user_id'] == $this->user_info['user_id']) ? 1 : 0;
        return;
    }

    /**
     * 转正流程信息
     */
    private function get_user_regular_info(){
        $res = $this->bll['user_regular']->get_user_regular_by_flow_id($this->params['flow_id']);
        if( empty($res) ){
            $this->show_tips('ERROR_SYSTEM', Flow_Urls::flow_list());
        }else{
            $this->data['flow_info'] = $res;
        }
    }

    /**
     * 用户信息
     */
    private function get_userinfo(){
        $res = $this->bll['user_regular']->get_userinfo_by_user_id($this->data['flow_info']['user_id']);
        if( empty($res) ){
            $this->show_tips('ERROR_SYSTEM', Flow_Urls::flow_list());
        }else{
            $this->data['user_info'] = $res;
        }
    }


    /**
     * 提交表单后处理数据库
     */
    private function operate_db(){

        $this->update_user_regular();

        $this->update_flow();

        $this->update_apply_relation();

        $this->insert_operate_log();

        //数据库操作完成后跳转页面
        $this->response->redirect(Flow_Urls::flow_list());
    }


    /**
     * 更新转正表
     */
    private function update_user_regular(){
        $data = array(
                'apply_status'=>Const_Flow::FLOW_STATUS_EFFECT,
                'super_opinion'=>$this->params['super_opinion'],
                'super_reason'=>$this->params['super_reason']
        );
        $this->bll['user_regular']->update_user_regular($this->params['flow_id'], $data);
    }

    /**
     * 更新flow表
     */
    private function update_flow(){
        $data = array(
            'flow_stage'=>Const_Flow::FLOW_REGULAR_2,
            'flow_opt'=>Const_Flow::PHASE_41,
            'flow_role_list'=>'',
            'now_status'=>Const_Flow::FLOW_STATUS_EFFECT,
                );
        $this->bll['user_regular']->update_flow($this->params['flow_id'], $data);
    }


    /**
     * 软删除apply_relation中该条flow的记录
     */
    private function update_apply_relation(){
        $bll_apply_relation = new Bll_Flow_ApplyRelation();
        $bll_apply_relation->update_by_flow($this->params['flow_id']);
    }

    /**
     * 插入operate_log
     */
    private function insert_operate_log(){
        $data = array(
                'flow_stage'=>Const_Flow::FLOW_REGULAR_2,
                'flow_opt'=>Const_Flow::PHASE_41,
                'flow_type'=>Const_Flow::FLOW_TYPE_REGULAR,
                'user_name'=>array_shift(explode('-', $this->params['user_name'])),
                'role_type'=>Const_Flow::ROLE_SUPERIOR,
                'opt_user_id'=>$this->user_info['user_id'],
                'flow_id'=>$this->params['flow_id'],
                'effect_date'=>$this->params['effect_date'],
                'user_id'=>$this->params['user_id'],
                'user_name'=>$this->params['user_name'],
                'work_city_id'=>$this->params['work_city_id']
                );
        $bll_operate_log = new Bll_Flow_OperateLog();
        $bll_operate_log->save_log($data);
    }
}